﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LdCms.BLL.Community
{
    using LdCms.EF.DbEntitiesContext;
    using LdCms.EF.DbModels;
    using LdCms.EF.DbStoredProcedure;
    using LdCms.IBLL.Community;
    using LdCms.IDAL.Community;
    using LdCms.Common.Json;
    using LdCms.Common.Security;
    using LdCms.Common.Extension;
    using LdCms.Common.Net;

    /// <summary>
    /// 
    /// </summary>
    public partial class SubscribeService:BaseService<Ld_Community_Subscribe>,ISubscribeService
    {
        private readonly ISubscribeDAL SubscribeDAL;
        private readonly LdCmsDbEntitiesContext LdCmsDbEntitiesContext;
        public SubscribeService(LdCmsDbEntitiesContext LdCmsDbEntitiesContext, ISubscribeDAL SubscribeDAL)
        {
            this.LdCmsDbEntitiesContext = LdCmsDbEntitiesContext;
            this.SubscribeDAL = SubscribeDAL;
            this.Dal = SubscribeDAL;
        }
        public override void SetDal()
        {
            Dal = SubscribeDAL;
        }

        public bool IsExists(int systemId, string companyId, string memberId, string subscribeUserId)
        {
            try
            {
                var expression = ExtLinq.True<Ld_Community_Subscribe>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.MemberID == memberId && m.SubscribeUserID == subscribeUserId);
                return IsExists(expression);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool SaveSubscribe(Ld_Community_Subscribe entity)
        {
            try
            {
                int systemId = entity.SystemID;
                string companyId = entity.CompanyID;
                string memberId = entity.MemberID;
                string subscribeUserId = entity.SubscribeUserID;
                var verify = IsExists(systemId, companyId, memberId, subscribeUserId);
                if (verify)
                    throw new Exception("已关注！");
                entity.IpAddress = Net.Ip;
                entity.State = true;
                entity.CreateDate = DateTime.Now;
                return Add(entity);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool SaveSubscribe(int systemId, string companyId, string memberId, string subscribeUserId)
        {
            try
            {
                var verify = IsExists(systemId, companyId, memberId, subscribeUserId);
                if (verify)
                    throw new Exception("已关注！");
                var entity = new Ld_Community_Subscribe()
                {
                    SystemID = systemId,
                    CompanyID = companyId,
                    MemberID = memberId,
                    SubscribeUserID = subscribeUserId,
                    IpAddress = Net.Ip,
                    State = true,
                    CreateDate = DateTime.Now
                };
                return Add(entity);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool DeleteSubscribe(int systemId, string companyId, string memberId, string subscribeUserId)
        {
            try
            {
                var expression = ExtLinq.True<Ld_Community_Subscribe>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.MemberID == memberId && m.SubscribeUserID == subscribeUserId);
                return Delete(expression);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


    }
}
